#! /bin/bash

buildWidgetsDir=build/remoteComponents/widgets
dir=$(ls -l ../../"UI/UI Flows/Widget" | awk '/^d/ {print $NF}')
echo Find widgets: $dir
for i in $dir; do
    echo deal with $i...
    name=$(echo $i | sed -e "s/\b\(.\)/\u\1/g")
    echo $name
    mkdir ./$buildWidgetsDir/$name
    cp ./$buildWidgetsDir/$name.js ./$buildWidgetsDir/$name/index.js
    items=$(ls ../../"UI/UI Flows/Widget"/$i)
    echo Find $i widget content: $items
    cp ./public/images/cover.png ./$buildWidgetsDir/$name/cover.png
    cp ./public/images/icon.png ./$buildWidgetsDir/$name/icon.png
    for item in $items; do
        echo $item
        if grep -q c_ <<<$item; then
            cp -f ../../"UI/UI Flows/Widget"/$i/$item ./$buildWidgetsDir/$name/cover.png
        fi
        if grep -q i_ <<<$item; then
            cp -f ../../"UI/UI Flows/Widget"/$i/$item ./$buildWidgetsDir/$name/icon.png
        fi
    done
    desc=$(grep -w 'description' ../../UI/"UI Flows"/Widget/$i/var.json | grep -oP '(?<=: ").*(?=")')
    xml='<?xml version="1.0" encoding="UTF-8"?>\n
<Res>\n
<name>lcap_widget_'$i'</name>\n
<desc>'$desc'</desc>\n
<key>lcap_widget_remote_'$i'</key>\n
<tag>lcap_widget_zmp</tag>\n
</Res>'
    echo -e $xml >>./$buildWidgetsDir/$name/component.xml
    zip -j $name.zip ./$buildWidgetsDir/$name/*
    echo updating widget--------------------------------------
    echo update 8990 $name.zip...    
    curl -X POST "http://10.10.168.57:8990/mat/ressimple/upload" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "file=@$name.zip;type=application/x-zip-compressed"
    echo update 8991 $name.zip...
    curl -X POST "http://10.10.168.57:8991/mat/ressimple/upload" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "file=@$name.zip;type=application/x-zip-compressed"
    echo ----------------------------------------------
done
